<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.js"></script>
    <script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.min.js"></script>
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
     <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css">
     <script src="http://cdn.bootcss.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
    <title>p35-39品牌数据库</title>
</head>
<body>
    <div id="app">
        
        <div class="panel panel-primary">
              <div class="panel-heading">
                    <h3 class="panel-title">添加品牌</h3>
              </div>
              <div class="panel-body form-inline">
                  <label>
                        Name:
                        <input type="text" v-model="name" class="form-control">
                    </label>
                    <input type="button" @click="add" value="添加" class="btn btn-primary">
              </div>
        </div>
        
        <table class="table table-hover table-bordered table-striped">
            <thead>
                <tr>
                    <th>Id</th>
                    <th>Name</th>
                    <th>Ctime</th>
                    <th>Operation</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="item in list" :key="item.id">
                    <td>{{item.id}}</td>
                    <td>{{item.title}}</td>
                    <td>{{item.createDate}}</td>
                    <td><a href="#" @click.prevent="del(item.id)" class="btn btn-primary">删除</a></td>
                </tr>
            </tbody>
        </table>
        
    </div>
    <script>
        Vue.http.options.root="http://127.0.0.1:8080";
        //全局启用emulateJSON选项
        Vue.http.options.emulateJSON=true;
        var vm=new Vue({
            el:"#app",
            created(){
                this.getAllList();
            },
            data:{
                name:"",
                list:[
                ]
            },
            methods:{
                del(id){
                    this.$http.get("weapp/vue/deleteInfo?id="+id).then(res=>{
                        if(res.body.ret==0){
                            this.getAllList();
                        }else{
                            alert("删除失败!");
                        }
                    })
                },
                add(){
                    console.log(this.name)
                    this.$http.post("weapp/vue/addInfo",{name:this.name},{}).then(res=>{
                        if(res.body.ret==0){
                            this.getAllList();
                            this.name="";
                        }else{
                            alert("添加失败");
                        }
                    })
                },
                getAllList(){
                    this.$http.get("weapp/vue/getAllInfo").then (function(res){
                        console.log(res)
                        if(res.status==200){
                            this.list=res.body.data;
                        }else{
                            alert("获取数据失败!");
                        }
                    })
                }
            },
            filters:{},
            directives:{}
        })
    </script>
</body>
</html>